﻿//---*Authenticity Seal
/*			                                _ .--.
					                       ( `    )
									     .-'      `--,
			                  _..----.. (             )`-.
			                .'_|` _|` _|(  .__,           )
						   /_|  _|  _|  _(        (_,  .-'
			              ;|  _|  _|  _|  '-'__,--'`--'
			              | _|  _|  _|  _| |
			          _   ||  _|  _|  _|  _|
			        _( `--.\_|  _|  _|  _|/
			      .-'       )--,|  _|  _|.`
				(__, (_      ) )_|  _| /
			      `-.__.\ _,--'\|__|__/
			                    ;____;
			                     \  /
								  OO
			                     |""|
			                     '=='
			                     
           			 Autor: Federico Angel Donnet
*/
//---*Authenticity Seal
//---*Esta clase controla y administra los diferentes emisores de particulas.
using UnityEngine;
using System.Collections;
//---*Para usar Listas!
using System.Collections.Generic;
public class EmitterFactoryControllerScript : MonoBehaviour
{
	//----------------//
	//---*Atributos:
	//----------------//
	//---*Instancia unica Singleton:
	private static 				EmitterFactoryControllerScript		instance	=	null;
	//---*Vector de EmittersPackages:
	[HideInInspector]
	public	GameObject[]		EmittersPackages								=	null;
	//---*Lista que va a almacenar las referencias de estos emitters instanciados:
	private	List<GameObject>	emittersPackages								=	null;
	//---*Transform del gameObject que va a hacer de parent de todos los SoundPackages:
	private	Transform			packagesParent									=	null;

	//----------------//
	//---*Metodos:
	//----------------//
	//---*Instancia Singleton static:
	public static EmitterFactoryControllerScript getInstance()
	{
		
		//---*Si la instancia no es nula, la devuelve
		//---*Pero si es nula, crea un gameObject con dicha instancia:
		if(instance	== null)
		{
			//---*Instancia un GameObject de nombre "EmitterFactoryControllerScript" y le aniade como componente
			//---*este Script Singleton:
			instance = new GameObject("EmitterFactoryInstance").AddComponent<EmitterFactoryControllerScript>(); 
			Debug.Log("[EMITTERFACTORY] Se Crea el EmitterFactory!!!");
			
		}
		return instance;
	}
	public	void Init()
	{
		instance = getInstance();
		Debug.Log("[EMITTERFACTORY] IM READY TO THE SHOW!");
	}
	// Use this for initialization
	void Start ()
	{

		//---*Si el packagesParent no existe:
		if(this.packagesParent == null)
		{
			this.packagesParent 									=	new GameObject("EmitterPackagesParent").transform;
			this.packagesParent.position							=	Vector3.zero;
			this.packagesParent.rotation							=	new Quaternion (0.0f,0.0f,0.0f,0.0f);
			this.packagesParent.transform.parent					=	this.gameObject.transform;
		}
		//---*Instancia la lista de emitters:
		if(this.emittersPackages == null)
		{
			this.emittersPackages = new List<GameObject>();
			//---*Recorremos los Emitter y los instanciamos en la lista "emitters":
			for(int n=0;n<this.EmittersPackages.Length;n++)
			{
				this.emittersPackages.Add(Instantiate(this.EmittersPackages[n]) as GameObject);
				this.emittersPackages[n].transform.position			=	Vector3.zero;
				this.emittersPackages[n].transform.rotation			=	new Quaternion (0.0f,0.0f,0.0f,0.0f);
				//---*Le decimos que sean childs del Transform del PackagesParent:
				this.emittersPackages[n].transform.parent			=	this.packagesParent.transform;	
			}
		}
	}

	//---*Activa el Emitter especificado (por nombre) en la posicion dada:
	public void PlayEmitter(string name,Vector3 pos)
	{
		//---*Recorre todos los EmittersPackages para buscar el deseado:
		for(int n = 0;n<this.emittersPackages.Count;n++)
		{
			if(this.emittersPackages[n].gameObject.GetComponent<EmitterPackage>().EmitterName == name)
			{
				this.emittersPackages[n].gameObject.GetComponent<EmitterPackage>().Play(pos);
				//---*Cortamos la busqueda:
				break;
			}
		}

	}
}
